//
//  AppDelegate+DTCAppService.m
//  BluetoothProject
//
//  Created by JianWei Chen on 16/9/5.
//  Copyright © 2016年 JianWei Chen. All rights reserved.
//

#import "AppDelegate+DTCAppService.h"
#import <KSCrash/KSCrashInstallationStandard.h>
#import "BlueContentModel.h"
#import "DoorSendCodeModel.h"
#import "RongGaoGit.h"
#import "DoorStatusModel.h"

@interface AppDelegate ()<DTCloudOperationDelegate>

@end

@implementation AppDelegate (DTCAppService)


- (void)setDTSDKManagerConfig
{
    [[DTCloudManager defaultJNI_iOS_SDK] setSanBox:NO];
    [[DTCloudManager defaultJNI_iOS_SDK] setServerURL:@"http://api.ourslinks.com/index.php"];

    [DTCloudManager defaultJNI_iOS_SDK].operationDelegate = self;
    
    NSLog(@"===%@",DTCloudKitAppId);
    [[DTCloudManager defaultJNI_iOS_SDK] startAppId:DTCloudKitAppId appKey:DTCloudKitAppKey successCallback:^(NSDictionary *dic) {
        NSLog(@"====%@",dic);
    } errorCallback:^(NSDictionary *dic) {
        NSLog(@"====%@",dic);
    }];
    
}

- (void)setNetworkBaseConfig
{
    //网络
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
    config.baseUrl = ServerURL;
}

- (void)setKSCrashConfig
{
    KSCrashInstallationStandard* installation = [KSCrashInstallationStandard sharedInstance];
    installation.url = [NSURL URLWithString:@"https://collector.bughd.com/kscrash?key=050e4c6d7a4de971d0d32116fb65b739"];
    [installation install];
    [installation sendAllReportsWithCompletion:nil];
}

- (void)sendCommandByFunctionCode:(NSString *)code functionCommand:(NSString*)command{
    
    if (self.doorWiFiModel) {
       
        [[DTCloudManager defaultJNI_iOS_SDK] sendCommandByFunctionCode:code functionCommand:command deviceMacAddress:self.doorWiFiModel.macAddress deviceType:DeviceUsedByWiFi];
        
    }
    
}

/**
 *  DTCloud收到设备的命令
 *
 *  @param backCode    返回状态码
 *  @param backContent 返回内容
 *  @param macAddress  设备的标识
 */
- (void)dtCloudManagerMessageReceiveBackCode:(NSString *)backCode backContent:(NSString *)backContent macAddress:(NSString *)macAddress{

//    if ([EXRACT_OBJECT(DeviceMac) isEqualToString:macAddress])
//    {
        [[WiFiReceiveDataManager defaultService] handleTheBackData:backContent andBackCode:backCode mac:macAddress];
    //}
    

    if ([[macAddress uppercaseString] isEqualToString:[AppDelegate globalDelegate].doorWiFiModel.macAddress]) {
        NSString *Content;
        Content = [backContent substringWithRange:NSMakeRange(backContent.length-2, 2)];
        
        
        DoorStatusModel *statusModel = [RongGaoGit getDoorStatusModel];
        [statusModel functionCode:[backCode uppercaseString] dataCode:Content];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"updateTableView" object:nil];
        if ([[backCode uppercaseString] isEqualToString:@"F801"]) {
            
            if ([Content isEqualToString:@"01"]) {//开门
                
                [[NSNotificationCenter defaultCenter]postNotificationName:@"openTheDoor" object:nil];
                
            }else if ([Content isEqualToString:@"02"]){//关门
                
                  [[NSNotificationCenter defaultCenter]postNotificationName:@"closeTheDoor" object:nil];
                
            }else if ([Content isEqualToString:@"03"]){//暂停
                
                [[NSNotificationCenter defaultCenter]postNotificationName:@"pauseTheDoor" object:nil];

            }
            
        }
    }
   
    
}



/**
 *  发送到DTCloud命令失败
 *
 *  @param cmdtag 失败的命令标识
 */

- (void)dtCloudManagerMessageSendFaild:(NSUInteger)cmdtag{
    
    
}

/**
 *  DTCloud发送的设备上线下线的回调
 *
 *  @param isonline     上线下线
 *  @param macAddress   设备的标识
 */
- (void)dtCloudManagerBackDeviceState:(BOOL)isonline macAddress:(NSString *)macAddress{
    
    DTDevice *info = [[DTDevice alloc] init];
    info.isOnline = isonline;
    info.macAddress = macAddress;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Deviceisonline" object:info];
    
}

@end
